package com.app.toknow;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.app.toknow.Activity.LoginActivity;
import com.app.toknow.Adapter.ViewPagerAdapter;
import com.app.toknow.Common.SharedPreferencesUtil;

import java.util.ArrayList;

public class WelcomeActivity extends Activity  implements View.OnClickListener,ViewPager.OnPageChangeListener {
    private static final String TAG = "WelcomeActivity" ;
    //定义ViewPager对象
    private ViewPager viewPager;
    //
    private LinearLayout linearLayout;
    //定义ViewPager适配器
    private ViewPagerAdapter vpAdapter;
    //定义一个ArrayList来存放View
    private ArrayList<View> views;
    //引导图片资源
    private static final int[] pics = {R.drawable.guide1, R.drawable.guide2,R.drawable.guide3,R.drawable.guide4};
    //底部小点的图片
    private ImageView[] points;
    //记录当前选中位置
    private int currentIndex;
    //进入主界面按钮
    private Button enterMainButton;
    //每次进入应用的欢迎界面
    private ImageView imageViewSplash;
    //
    private  SharedPreferences sharedPreferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome_guide);
        boolean isFirst = Boolean.parseBoolean(SharedPreferencesUtil.get(getApplicationContext(), "isFirstJoinApp", true).toString());
        Log.e(TAG,"isFirst ================"+isFirst);
        if (isFirst == true) {
            SharedPreferencesUtil.put(getApplicationContext(), "isFirstJoinApp", false);
            //应用首次启动
            initView();
            initData();
        } else {
            //应用非首次启动
            universalWelcome();
        }
    }

    /**
     * 初始化组件
     */
    private void initView(){
        //实例化ArrayList对象
        views = new ArrayList<View>();
        //实例化ViewPager
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        //实例化ViewPager适配器
        vpAdapter = new ViewPagerAdapter(views);
    }

    /**
     * 初始化数据
     */
    private void initData(){
        //定义一个布局并设置参数
        LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);

        //初始化引导图片列表
        for(int i=0; i<pics.length; i++) {
            ImageView iv = new ImageView(this);
            iv.setScaleType(ImageView.ScaleType.FIT_XY );
            iv.setLayoutParams(mParams);
            iv.setImageResource(pics[i]);
            views.add(iv);
        }
        //设置数据
        viewPager.setAdapter(vpAdapter);
        //设置监听
        viewPager.setOnPageChangeListener(this);
        //初始化底部小点
        initPoint();
    }

    /**
     * 初始化底部小点
     */
    private void initPoint(){
        linearLayout = (LinearLayout) findViewById(R.id.ll);
        points = new ImageView[pics.length];
        //循环取得小点图片
        for (int i = 0; i < pics.length; i++) {
            //得到一个LinearLayout下面的每一个子元素
            points[i] = (ImageView) linearLayout.getChildAt(i);
            //默认都设为灰色
            points[i].setEnabled(true);
            //给每个小点设置监听
            points[i].setOnClickListener(this);
            //设置位置tag，方便取出与当前位置对应
            points[i].setTag(i);
        }

        //设置当面默认的位置
        currentIndex = 0;
        //设置为白色，即选中状态
        points[currentIndex].setEnabled(false);
    }
    private void universalWelcome()
    {
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setVisibility(View.INVISIBLE);
        linearLayout = (LinearLayout) findViewById(R.id.ll);
        linearLayout.setVisibility(View.INVISIBLE);
        enterMainButton = (Button) findViewById(R.id.start);
        enterMainButton.setVisibility(View.INVISIBLE);
        imageViewSplash = (ImageView) findViewById(R.id.splash_imageview);
        imageViewSplash.setVisibility(View.VISIBLE);
        GoToNextActivity();
    }
    /**
     * 当滑动状态改变时调用
     */
    @Override
    public void onPageScrollStateChanged(int arg0) {
//        Log.d(TAG,"onPageScrollStateChanged ================"+arg0);
    }

    /**
     * 当前页面被滑动时调用
     */

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
//        Log.d(TAG, "onPageScrolled ==============="+arg2);
    }

    /**
     * 当新的页面被选中时调用
     */

    @Override
    public void onPageSelected(int position) {
        Log.d(TAG, "onPageSelected ==============="+position);
        //设置底部小点选中状态
        setCurDot(position);
    }

    /**
     * 通过点击事件来切换当前的页面
     */
    @Override
    public void onClick(View v) {
        int position = (Integer)v.getTag();
        setCurView(position);
        setCurDot(position);
    }

    /**
     * 设置当前页面的位置
     */
    private void setCurView(int position){
        if (position < 0 || position >= pics.length) {
            return;
        }
        viewPager.setCurrentItem(position);
    }

    /**
     * 设置当前的小点的位置
     */
    private void setCurDot(int positon) {
        if (positon < 0 || positon > pics.length - 1 || currentIndex == positon) {
            return;
        }
        points[positon].setEnabled(false);
        points[currentIndex].setEnabled(true);

        currentIndex = positon;
        Log.e(TAG, "currentIndex ==============="+positon);
        if(currentIndex == pics.length - 1)
             ShowEnterButton();
        else
        HideEnterButton();
    }

    private void HideEnterButton()
    {
        Log.e(TAG, "HideEnterButton");
        enterMainButton = (Button) findViewById(R.id.start);
        enterMainButton.setVisibility(View.INVISIBLE);
    }

    private void ShowEnterButton()
    {
        Log.e(TAG, "ShowEnterButton");
        enterMainButton = (Button) findViewById(R.id.start);
        enterMainButton.setVisibility(View.VISIBLE);
        enterMainButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                universalWelcome();
            }
        });
    }

    /**
     * 进入主界面
     */
    void GoToNextActivity() {
        new Handler().postDelayed(new Runnable() {
            @Override public void run() {
                boolean isLogin = Boolean.parseBoolean(SharedPreferencesUtil.get(getApplicationContext(),"isLoginApp",false).toString());
                Log.e(TAG, "isLoginApp ================" + isLogin);
                Intent intent;
                if (isLogin == false) {
                    //去登陆界面
                    intent = new Intent(WelcomeActivity.this, LoginActivity.class);
                } else {
                    //去主页界面
                    intent = new Intent(WelcomeActivity.this, MainActivity.class);
                }
                startActivity(intent);
                finish();
                overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
            }
        }, 1000 * 2);
    }
}
